import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {vehicleStatus} from '../Enum';
import Export from '../common/Export';
import {PERMISSION} from './constants';
import ImportPanel from './ImportPanel';
import {FORM_OPTIONS} from '../constants';
import TextArea from 'Shared/components/TextArea';
import {hasPermissions, ListTypeCL} from './utils';
import WrappedSelect from '../common/WrappedSelect';
import {Button, Form, Row, Col, Card, message} from 'antd';
import {injectIntl, language, formatMessage} from './intl';
import DateRangePicker from 'Shared/components/DateRangePicker';
const vehicleStatusList = vehicleStatus.toList(language);
const ButtonGroup = Button.Group;
const FormItem = Form.Item;
class QueryPanel extends PureComponent {
    state = {showImport: false}
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    importChange = data => {
        if(data) {
            this.setState({showImport: false});
            message.success(formatMessage({
                id: 'queryPanel.msg.import',
                defaultMessage: 'KD车辆导入成功'
            }));
        }
    }
    onImport = () => this.setState({showImport: true});
    onCloseImport = () => this.setState({showImport: false});
    render() {
        const {conditions, permissions, querySearch, onResetClick, loading} = this.props;
        const {brands} = this.props.initEnumData;
        const operations = [];
        if(hasPermissions(permissions, PERMISSION.export))
            operations.push(
                <Export key={PERMISSION.export} exportRequest={this.props.onClickExport}/>
            );
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            {brands && brands.length > 1 &&
                                <Col {...FORM_OPTIONS.col}>
                                    <FormItem
                                        label={formatMessage({
                                            id: 'queryPanel.form.brandId',
                                            defaultMessage: '品牌'
                                        })}
                                        {...FORM_OPTIONS.item}>
                                        <WrappedSelect
                                            allowClear
                                            name="brandId"
                                            value={conditions.brandId}
                                            options={brands}
                                            onChange={this.handleFilterChange}/>
                                    </FormItem>
                                </Col>
                            }
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.vin',
                                    defaultMessage: 'VIN'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="vin"
                                        value={conditions.vin}
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.placeholder',
                                            defaultMessage: '多个输入，以回车隔开'
                                        })}
                                        onBlur={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.productCode',
                                    defaultMessage: '物料号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="productCode"
                                        value={conditions.productCode}
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.placeholder',
                                            defaultMessage: '多个输入，以回车隔开'
                                        })}
                                        onBlur={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.productName',
                                    defaultMessage: '物料名称'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="productName"
                                        value={conditions.productName}
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.placeholder',
                                            defaultMessage: '多个输入，以回车隔开'
                                        })}
                                        onBlur={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.productCategoryCode',
                                    defaultMessage: '车型编号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="productCategoryCode"
                                        value={conditions.productCategoryCode}
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.placeholder',
                                            defaultMessage: '多个输入，以回车隔开'
                                        })}
                                        onBlur={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.productCategoryName',
                                    defaultMessage: '车型名称'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="productCategoryName"
                                        value={conditions.productCategoryName}
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.placeholder',
                                            defaultMessage: '多个输入，以回车隔开'
                                        })}
                                        onBlur={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.engineCode',
                                    defaultMessage: '发动机号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="engineCode"
                                        value={conditions.engineCode}
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.placeholder',
                                            defaultMessage: '多个输入，以回车隔开'
                                        })}
                                        onBlur={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.status',
                                    defaultMessage: '状态'
                                })}{...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="status"
                                        value={conditions.status}
                                        options={vehicleStatusList}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.rolloutDate',
                                        defaultMessage: '下线日期'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="rolloutDate"
                                        value={conditions.rolloutDate}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={querySearch} loading={loading}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.query',
                                        defaultMessage: '查询'
                                    })}</Button>
                                <Button key="clear" onClick={onResetClick}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.reset',
                                        defaultMessage: '重置'
                                    })}</Button>
                            </Col>
                            <Col span={18} className="col-align-right">
                                {(hasPermissions(permissions, PERMISSION.import) &&
                                        <Button key="import" onClick={this.onImport}>{formatMessage({
                                            id: 'queryPanel.btn.import',
                                            defaultMessage: 'KD车辆导入'
                                        })}</Button>)}
                                <ButtonGroup>{operations}</ButtonGroup>
                            </Col>
                        </Row>
                        {this.state.showImport &&
                        <ImportPanel
                            onCancel={this.onCloseImport}
                            onImportChange={this.importChange} />
                        }
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    initEnumData: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    intl: PropTypes.object,
};
import {connect} from 'react-redux';
import {querySearch, resetQueryPanel, saveQueryCondition, exportData} from './actions';
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const brands = initData.brands ? ListTypeCL(initData.brands) : [];
        return {
            brands, // 品牌
            initData
        };
    }
);

const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    initEnumData: getinitEnumData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onClickExport: () => dispatch(exportData()),
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
